import type {
  AddReq,
  DelReq,
  EditReq,
  InfoReq,
  UserPageRes,
} from '@fast-crud/fast-crud';

import type { AxiosRequestConfig } from '@vben/request';

import type { BaseEmployeeModel } from './model/baseEmployeeModel';

import type { PageParams, PageResult } from '#/api';

import { requestClient } from '#/api/request';
import { ServicePrefixEnum } from '#/enums/commonEnum';

const MODULAR = 'baseEmployee';

const ServicePrefix = ServicePrefixEnum.BASE;

export const BaseEmployeeConfig = {
  Save: {
    url: `${ServicePrefix}/${MODULAR}`,
    method: 'POST',
  } as AxiosRequestConfig,
  Update: {
    url: `${ServicePrefix}/${MODULAR}`,
    method: 'PUT',
  } as AxiosRequestConfig,
};

export namespace BaseEmployeeApi {
  /**
   * 根据条件查询分页数据
   */
  export async function page(
    params: PageParams<BaseEmployeeModel.BaseEmployeePageQuery>,
  ) {
    return requestClient.post<
      PageResult<BaseEmployeeModel.BaseEmployeeResultVO>
    >(`${ServicePrefix}/${MODULAR}/page`, params);
  }
  /**
   * 根据id查询单条数据
   */
  export async function get(id: string) {
    return requestClient.get<BaseEmployeeModel.BaseEmployeeResultVO>(
      `${ServicePrefix}/${MODULAR}/${id}`,
    );
  }
  // 查询员工拥有的角色
  export async function findEmployeeRoleByEmployeeId(employeeId: string) {
    return requestClient.get<string[]>(
      `${ServicePrefix}/${MODULAR}/findEmployeeRoleByEmployeeId`,
      { params: { employeeId } },
    );
  }

  // 给员工绑定角色

  export async function saveEmployeeRole(
    params: BaseEmployeeModel.BaseEmployeeRoleRelSaveVO,
  ) {
    return requestClient.post<string[]>(
      `${ServicePrefix}/${MODULAR}/employeeRole`,
      params,
    );
  }

  /**
   * 保存
   */
  export async function save(params: BaseEmployeeModel.BaseEmployeeSaveVO) {
    return requestClient.post<BaseEmployeeModel.BaseEmployeeResultVO>(
      BaseEmployeeConfig.Save.url as string,
      params,
    );
  }

  /**
   * 修改
   */
  export async function update(params: BaseEmployeeModel.BaseEmployeeUpdateVO) {
    return requestClient.put<BaseEmployeeModel.BaseEmployeeResultVO>(
      BaseEmployeeConfig.Update.url as string,
      params,
    );
  }

  /**
   * 删除
   */
  export async function remove(data: string[]) {
    return requestClient.delete<boolean>(`${ServicePrefix}/${MODULAR}`, {
      data,
    });
  }

  /** 运营者将非该企业的用户，添加进企业使其成为员工并设置为租户管理员 */
  export async function bindUser(
    params: BaseEmployeeModel.DefTenantBindUserVO,
  ) {
    return requestClient.post<boolean>(
      `${ServicePrefix}/${MODULAR}/bindUser`,
      params,
    );
  }

  /** 运营者通过租户申请，并将申请者设为管理员 */
  export async function toExamineTenantAndBindUser(
    params: BaseEmployeeModel.DefTenantBindUserVO,
  ) {
    return requestClient.post<boolean>(
      `${ServicePrefix}/${MODULAR}/toExamineTenantAndBindUser`,
      params,
    );
  }

  /** 租户管理员邀请用户进入企业 */
  export async function invitationUser(
    params: BaseEmployeeModel.DefTenantBindUserVO,
  ) {
    return requestClient.post<boolean>(
      `${ServicePrefix}/${MODULAR}/invitationUser`,
      params,
    );
  }

  /** 运营者将用户设置为某个租户的租户管理员 */
  export async function bindTenantAdmin(
    params: BaseEmployeeModel.DefTenantBindUserVO,
  ) {
    return requestClient.post<boolean>(
      `${ServicePrefix}/${MODULAR}/bindTenantAdmin`,
      params,
    );
  }

  export async function findByIds(ids: string[]) {
    return requestClient.post<BaseEmployeeModel.BaseEmployeeResultVO[]>(
      `${ServicePrefix}/${MODULAR}/findByIds`,
      ids,
    );
  }

  // fast-crud 需要的接口
  export const pageRequest = async (
    pageQuery: PageParams<BaseEmployeeModel.BaseEmployeePageQuery>,
  ): Promise<UserPageRes> => {
    return await page(pageQuery);
  };

  export const infoRequest = async (ctx: InfoReq): Promise<UserPageRes> => {
    const { mode, row } = ctx;
    if (mode === 'view') {
      // 请求后台查询数据
      const record = await get(row.id);

      return { ...record?.defUser, ...record };
    }
    return row;
  };

  export const addRequest = async (req: AddReq) => {
    const { form } = req;
    return await save(form);
  };

  export const editRequest = async (ctx: EditReq) => {
    const { form } = ctx;
    return await update(form);
  };

  export const delRequest = async (ctx: DelReq) => {
    const { row } = ctx;
    return await remove([row.id]);
  };
}
